package com.cj.bus.common.utils;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.cj.bus.common.entity.tree.MultiTreeNode;
import com.cj.bus.common.entity.tree.SingleTreeNode;

public final class ToMultiTreeTransformerUtil {
	
	public static void main(String[] args) {
		List<SingleTreeNode> singleTreeNodes = new ArrayList<SingleTreeNode>();

		SingleTreeNode node = new SingleTreeNode();
		node.setId(1);
		node.setPId(0);
		node.setText("机械");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(2);
		node.setPId(0);
		node.setText("资源");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(3);
		node.setPId(0);
		node.setText("机床");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(4);
		node.setPId(1);
		node.setText("工业机械");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(5);
		node.setPId(1);
		node.setText("家庭机械");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(6);
		node.setPId(1);
		node.setText("汽车机械");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(7);
		node.setPId(4);
		node.setText("炼油器");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(8);
		node.setPId(4);
		node.setText("水管");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(9);
		node.setPId(6);
		node.setText("电动车");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(10);
		node.setPId(6);
		node.setText("大卡车");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(11);
		node.setPId(2);
		node.setText("技术专家");
		singleTreeNodes.add(node);
		
		node = new SingleTreeNode();
		node.setId(12);
		node.setPId(3);
		node.setText("磨具");
		singleTreeNodes.add(node);
		
		List<MultiTreeNode> resultList = transform(singleTreeNodes);
		
		System.out.print(JSONObject.toJSONString(resultList));
		
	}
	public static List<MultiTreeNode> transform(List<SingleTreeNode> singleTreeNodes){
		List<MultiTreeNode> multiTreeNodes = new ArrayList<>();
		// 先找出所有的根节点 
        for (int i = 0; i < singleTreeNodes.size(); i++) {
            SingleTreeNode singleTreeNode = singleTreeNodes.get(i);
            Boolean isRoot = true;
            for (int j = 0; j < singleTreeNodes.size(); j++) {
                SingleTreeNode temp = singleTreeNodes.get(j);
                if (singleTreeNode.getPId() == temp.getId()) {
                    isRoot = false;
                    break;
                }
            }
            if (isRoot) {
                MultiTreeNode multiTreeNode = new MultiTreeNode(singleTreeNode.getId(),  
                            singleTreeNode.getText());
                multiTreeNodes.add(multiTreeNode);
            }
        }
        // 设置子节点 
        for (int i = 0; i < multiTreeNodes.size(); i++) {
            MultiTreeNode multiTreeNode = multiTreeNodes.get(i);
            setChildren(multiTreeNode,singleTreeNodes);
        }
        return multiTreeNodes;
	}
	
	/** 
	   * 设置子节点 
	   * @param multiTreeNode 
	   */
	    private static void setChildren(MultiTreeNode multiTreeNode,List<SingleTreeNode> singleTreeNodes){
	        for (int j = 0; j < singleTreeNodes.size(); j++) {
	            SingleTreeNode temp = singleTreeNodes.get(j);
	            if (temp.getPId() == multiTreeNode.getId()) {
	                MultiTreeNode child = new MultiTreeNode(temp.getId(),  
	                            temp.getText());
	                List<MultiTreeNode> children = multiTreeNode.getNodes();
	                if (children == null) {
	                    children = new ArrayList<>();
	                    multiTreeNode.setNodes(children);
	                }
	                children.add(child);
	                setChildren(child,singleTreeNodes);
	            }
	        }
	    }

}
